/**
 * @Title: HomeActivity.java
 * @Package: com.jason.fingerprint.ui
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月19日 上午8:19:23
 * @Version: V1.0
 */
package com.jason.fingerprint.ui;

import java.nio.MappedByteBuffer;
import java.util.List;

import org.kymjs.aframe.database.KJDB;
import org.kymjs.aframe.http.HttpCallBack;
import org.kymjs.aframe.http.KJFileParams;
import org.kymjs.aframe.http.KJHttp;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.google.gson.GsonBuilder;
import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.R;
import com.jason.fingerprint.adapter.HomeModelAdapter;
import com.jason.fingerprint.beans.OrganBean;
import com.jason.fingerprint.beans.OrganResp;
import com.jason.fingerprint.beans.ui.ModelBean;
import com.jason.fingerprint.configs.HttpConfig;
import com.jason.fingerprint.utils.CodecUtils;

/**
 * @ClassName: HomeActivity
 * @Description: 主页
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月19日 上午8:19:23
 */
public class HomeActivity extends Activity {
	
	private static final String TAG = "HomeActivity";

	private GridView mGridView;
	
	private HomeModelAdapter mHomeModelAdapter;
	private AppContext mAppContext;
	private KJDB mKjdb;
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		mAppContext = (AppContext)getApplication();
		mKjdb = mAppContext.getKjdb();
		mGridView = (GridView) findViewById(R.id.home_gridview);
		mHomeModelAdapter = new HomeModelAdapter(this);
		mGridView.setAdapter(mHomeModelAdapter);
		mGridView.setOnItemClickListener(new HomeModelItemClickListener());
	}
	
	private class HomeModelItemClickListener implements OnItemClickListener{

		/* (non-Javadoc)
		 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
		 */
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ModelBean bean = (ModelBean) parent.getItemAtPosition(position);
			Toast.makeText(getApplicationContext(), bean.getTitle(), Toast.LENGTH_SHORT).show();
			switch (bean.getIconId()) {
			case R.drawable.module_icon_luru:
				//TODO 指纹录入
				break;
			case R.drawable.module_icon_tongbu:
				//TODO 档案同步
				datbRecordSync();
				break;
			case R.drawable.module_icon_chaxun:
				//TODO 档案查询
				break;
			case R.drawable.module_icon_fuwu:
				//TODO 社区服务
				break;
			case R.drawable.module_icon_xuexi:
				//TODO 教育学习
				break;
			case R.drawable.module_icon_baodao:
				//TODO 日常报到
				break;
			case R.drawable.module_icon_zoufang:
				//TODO 工作走访
				break;
			case R.drawable.module_icon_qingjia:
				//TODO 外出请假
				break;
			case R.drawable.module_icon_tongzhi:
				//TODO 工作通知
				workNotice();
				break;
			case R.drawable.module_icon_tixing:
				//TODO 工作提醒
				workRemind();
				break;
			case R.drawable.module_icon_peizhi:
				//TODO 系统配置
				break;
			case R.drawable.module_icon_women:
				//TODO 关于我们
				startActivity(new Intent(HomeActivity.this,MainActivity.class));
				break;
				
			default:
				break;
			}
		}
		
	}
	
	//工作通知
	private void workNotice(){
		KJFileParams params = new KJFileParams();
		params.put("imie", "860312023914656");
		params.put("currentIndex", "1");
		params.put("registeredId", "100000");
		params.put("contentUrl", "http://www.baidu.com");
		String url = HttpConfig.POST_URL_WORKINGREMIND_QUERYREMIND;
		Log.i(TAG, "----->API:" + url);
		KJHttp kjHttp = new KJHttp();
		kjHttp.post(url, params, new HttpCallBack() {
			
			@Override
			public void onSuccess(Object arg0) {
				//String result = CodecUtils.base64Decode(arg0.toString());
				Log.i(TAG, "----->Result:" + arg0.toString());
				//OrganResp organResp = new GsonBuilder().create().fromJson(result, OrganResp.class);
				//Log.i(TAG, "----->organResp:" + organResp.toString());
			}
			
			@Override
			public void onLoading(long arg0, long arg1) {
				Log.i(TAG, "----->Result:arg0:" + arg0 + ",arg1:" + arg1);
			}
			
			@Override
			public void onFailure(Throwable arg0, int arg1, String arg2) {
				Log.i(TAG, "----->Result:arg1:" + arg1 + ",arg2:" + arg2);
			}
		});
	}
	
	//工作提醒
	private void workRemind(){
		KJFileParams params = new KJFileParams();
		params.put("imie", "860312023914656");
		String url = HttpConfig.POST_URL_WORKINGREMIND_QUERYREMIND;
		Log.i(TAG, "----->API:" + url);
		KJHttp kjHttp = new KJHttp();
		kjHttp.post(url, params, new HttpCallBack() {
			
			@Override
			public void onSuccess(Object arg0) {
				//String result = CodecUtils.base64Decode(arg0.toString());
				Log.i(TAG, "----->Result:" + arg0.toString());
				//OrganResp organResp = new GsonBuilder().create().fromJson(result, OrganResp.class);
				//Log.i(TAG, "----->organResp:" + organResp.toString());
			}
			
			@Override
			public void onLoading(long arg0, long arg1) {
				Log.i(TAG, "----->Result:arg0:" + arg0 + ",arg1:" + arg1);
			}
			
			@Override
			public void onFailure(Throwable arg0, int arg1, String arg2) {
				Log.i(TAG, "----->Result:arg1:" + arg1 + ",arg2:" + arg2);
			}
		});
	}
	
	//档案同步
	private void datbRecordSync(){
		KJFileParams params = new KJFileParams();
		params.put("imie", "860312023914656");
		params.put("currentIndex", "1");
		params.put("machId", "100000");
		String url = HttpConfig.POST_URL_DATB_ORGANSYNC;
		Log.i(TAG, "----->API:" + url);
		KJHttp kjHttp = new KJHttp();
		kjHttp.post(url, params, new HttpCallBack() {
			
			@Override
			public void onSuccess(Object arg0) {
				String result = CodecUtils.base64Decode(arg0.toString());
				Log.i(TAG, "----->Result:" + result);
				OrganResp organResp = new GsonBuilder().create().fromJson(result, OrganResp.class);
				//存入数据库
				if (organResp != null) {
					List<OrganBean> beans = organResp.getValues();
					if (beans != null) {
						int size = beans.size();
						if (size > 0) {
							try {
								mKjdb.save(beans);
							} catch (Exception e) {
								
							}
							beans = mKjdb.findAll(OrganBean.class);
							if (beans == null || beans.size() < 1) {
								return;
							}else{
								for (int i = 0; i < beans.size(); i++) {
									Log.i(TAG, "----->organ:" + beans.get(i).toString());
								}
							}
							
						}
					}
				}
				Log.i(TAG, "----->organResp:" + organResp.toString());
			}
			
			@Override
			public void onLoading(long arg0, long arg1) {
				Log.i(TAG, "----->Result:arg0:" + arg0 + ",arg1:" + arg1);
			}
			
			@Override
			public void onFailure(Throwable arg0, int arg1, String arg2) {
				Log.i(TAG, "----->Result:arg1:" + arg1 + ",arg2:" + arg2);
			}
		});
	}

}
